package com.study.hadoop.mapreduce.wordcount2;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Author huang
 * @Date 2022/1/2 23:57
 * @Version 1.0
 * 泛型含义：
 * 1.输入的key的类型 （每行第一个字符在文件中的index）
 * 2.输入的value的类型 (整行)
 * 3.输出的key的类型
 * 4.输出的value的类型
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text text = new Text();
    private IntWritable count = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        //获取一行
        String line = value.toString();
        //分割
        String[] words = line.split(" ");
        //遍历
        for (String word : words) {
            text.set(word);
            context.write(text, count);
        }
    }
}
